1795G - Removal Sequences - CodeForces Solution


bitmasks dfs and similar graphs *2700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define ff first 
#define ss second
#define pb push_back
#define all(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef unsigned long long ull;

void solve(){
    int n,m;
    cin >> n >> m;
    vi a(n+1);
    rep(i,1,n+1){
        cin >> a[i];
    }
    vector<vi> g(n+1,vi());
    rep(i,0,m){
        int a,b;
        cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }
    queue<int> q;
    vector<pii> ed;
    rep(i,1,n+1){
        a[i] = sz(g[i]) - a[i];
        if(a[i]==0)q.push(i),a[i]=-1;
    }
    while(!q.empty()){
        int v = q.front();
        q.pop();
        a[v]=-1;
        for(auto to : g[v])if(a[to]!=-1){
            ed.pb({v,to});
            a[to]--;
            if(a[to]==0)q.push(to);
        }
    }
    ll res = n*(ll)(n-1)/2 + n;
    vector<ull> mask(n+1);
    for(int l=1;l<=n;l+=64){
        int r = min(n,l+63);
        for(int i=l;i<=r;i++)mask[i] = 1LL<<(i-l);
        for(auto &e : ed){
            mask[e.ss]|=mask[e.ff];
        }
        for(int i=1;i<=n;i++){
            res -= __builtin_popcountll(mask[i]);
            mask[i]=0;
        }
    }
    cout << res << endl;
}

int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

1713C - Build Permutation
1699A - The Third Three Number Problem
1617B - GCD Problem
841A - Generous Kefa
1690B - Array Decrements
1692C - Where's the Bishop
104A - Blackjack
1438A - Specific Tastes of Andre
1711C - Color the Picture
1194C - From S To T
110B - Lucky String
1114A - Got Any Grapes
224B - Array
125B - Simple XML
567B - Berland National Library
431B - Shower Line
282C - XOR and OR
1582B - Luntik and Subsequences
609A - Флеш-карты
1207A - There Are Two Types Of Burgers
371C - Hamburgers
343B - Alternating Current
758B - Blown Garland
1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess
1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points